"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 主备数据库重建，重建时ctrl+c，再次重建；
Create At   : 2020/07/20
@zou_jialiang0505328114
Description :
    1.备机重建
    2.备机数据库重建过程种crtl+c
    3.备机数据库再次重建
Expect      :
    1.备机开始重建
    2.备机数据库重建过程种crtl+c成功
    3.备机数据库再次重建成功
History     :
    modified：2021/02/02  modified by @peilinqian，修改整个脚本逻辑
"""
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class DbsysCase160(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----Opengauss_Reliability_Dbsys_Case160 start----')
        self.log = Logger()
        self.log.info('----this is setup----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.sta1_root = Node(node='Standby1Root')
        self.com = Common()
        self.constant = Constant()

    def test_main(self):
        self.log.info('----1、备机进行重建----')
        success_msg = self.constant.REBUILD_SUCCESS_MSG
        build_thread = ComThread(self.sta_1_sh.executeGsctl,
                                 args=('build', success_msg,))
        build_thread.setDaemon(True)
        build_thread.start()
        time.sleep(10)

        self.log.info('----2、备机数据库重建过程种crtl+c成功----')
        build_process_cmd = 'ps ux | grep \'gs_ctl\'| grep -v grep'
        self.log.info(build_process_cmd)
        build_process_result = self.sta1_dbuser.sh(build_process_cmd).result()
        self.log.info(build_process_result)
        self.com.kill_pid_keyword('gs_ctl', 2, self.sta1_dbuser)
        time.sleep(1)
        build_process_cmd = 'ps ux | grep \'gs_ctl\'| grep -v grep'
        self.log.info(build_process_cmd)
        build_process_result = self.sta1_dbuser.sh(build_process_cmd).result()
        self.log.info(build_process_result)

        self.log.info('----备机重建结果----')
        build_thread.join()
        build_result = build_thread.get_result()
        self.assertFalse(build_result)

        self.log.info('----3、备机再次进行重建----')
        rebuild_result = self.sta_1_sh.build_fail_scp_crtfile()
        if rebuild_result:
            self.log.info('重建失败后，再次进行备机重建成功，已恢复')
        else:
            self.log.info('重建失败后，再次进行备机重建失败，请检查失败原因')
        self.assertTrue(rebuild_result)

        self.log.info('----查看主机query，同步是否正常----')
        self.pri_sh.check_location_consistency('primary', 3)

        self.log.info('----查询主备状态----')
        status = self.pri_sh.getDbClusterStatus('status')
        self.assertTrue(status)

    def tearDown(self):

        self.log.info('----Opengauss_Reliability_Dbsys_Case160 执行结束----')
